package com.zzdx.deployment.web.controller.system;

import com.zzdx.deployment.common.annotation.Log;
import com.zzdx.deployment.common.base.AjaxResult;
import com.zzdx.deployment.common.enums.BusinessType;
import com.zzdx.deployment.common.page.TableDataInfo;
import com.zzdx.deployment.common.utils.ExcelUtil;
import com.zzdx.deployment.framework.util.ShiroUtils;
import com.zzdx.deployment.framework.web.base.BaseController;
import com.zzdx.deployment.system.domain.ProductVersion;
import com.zzdx.deployment.system.service.IProductService;
import com.zzdx.deployment.system.service.IProductVersionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 产品版本 信息操作处理
 * 
 * @author mason
 * @date 2018-12-25
 */
@Controller
@RequestMapping("/system/productVersion")
public class ProductVersionController extends BaseController
{
    private String prefix = "system/productVersion";
	
	@Autowired
	private IProductVersionService productVersionService;
	@Autowired
	private IProductService productService;
	
	@RequiresPermissions("system:productVersion:view")
	@GetMapping()
	public String productVersion()
	{
	    return prefix + "/productVersion";
	}
	
	/**
	 * 查询产品版本列表
	 */
	@RequiresPermissions("system:productVersion:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProductVersion productVersion)
	{
		startPage();
        List<ProductVersion> list = productVersionService.selectProductVersionList(productVersion);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出产品版本列表
	 */
	@RequiresPermissions("system:productVersion:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductVersion productVersion)
    {
    	List<ProductVersion> list = productVersionService.selectProductVersionList(productVersion);
        ExcelUtil<ProductVersion> util = new ExcelUtil<ProductVersion>(ProductVersion.class);
        return util.exportExcel(list, "productVersion");
    }
	
	/**
	 * 新增产品版本
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.put("products",productService.selectProductAll());
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存产品版本
	 */
	@RequiresPermissions("system:productVersion:add")
	@Log(title = "产品版本", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProductVersion productVersion)
	{
		String loginName = ShiroUtils.getLoginName();
		productVersion.setCreateBy(loginName);
		productVersion.setCreateTime(new Date());
		return toAjax(productVersionService.insertProductVersion(productVersion));
	}

	/**
	 * 修改产品版本
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		ProductVersion productVersion = productVersionService.selectProductVersionById(id);
		mmap.put("productVersion", productVersion);
		mmap.put("products",productService.selectProductAll());
		return prefix + "/edit";
	}
	
	/**
	 * 修改保存产品版本
	 */
	@RequiresPermissions("system:productVersion:edit")
	@Log(title = "产品版本", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProductVersion productVersion)
	{
		String loginName = ShiroUtils.getLoginName();
		productVersion.setUpdateBy(loginName);
		productVersion.setUpdateTime(new Date());
		return toAjax(productVersionService.updateProductVersion(productVersion));
	}
	
	/**
	 * 删除产品版本
	 */
	@RequiresPermissions("system:productVersion:remove")
	@Log(title = "产品版本", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(productVersionService.deleteProductVersionByIds(ids));
	}

	/**
	 * 加载产品版本列表树数据
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData()
	{
		List<Map<String, Object>> tree = productVersionService.selectProductVersionTree();
		return tree;
	}

	/**
	 * 选择产品版本树页面
	 */
	@GetMapping("/selectProductVersionTree")
	public String selectDeptTree(ModelMap mmap)
	{
		return prefix + "/tree";
	}
}
